package com.juicygames.shmup.world;

import android.content.Context;
import android.graphics.Canvas;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;

import com.juicygames.framework.action.DrawAction;
import com.juicygames.framework.action.UpdateAction;
import com.juicygames.framework.draw.ContextProperty;
import com.juicygames.framework.draw.drawservice.DrawService;
import com.juicygames.framework.gamepart.GameWorld;
import com.juicygames.framework.touch.EntityTouchService;
import com.juicygames.framework.touch.TouchAction;
import com.juicygames.shmup.badguy.BadGuy;
import com.juicygames.shmup.fps.FpsDisplayer;
import com.juicygames.shmup.player.Player;
import com.juicygames.shmup.target.Target;

public class World extends GameWorld {
	private static final String LOGTAG = World.class.getName();
	
	Player player;
	DrawService drawService;
	EntityTouchService entityTouchService;
	
	public void load(Context context) {
		
		addPart(new ContextProperty(context));
		
		drawService = addPart(new DrawService());
		
		addPart(new FpsDisplayer());
		
		entityTouchService = addPart(new EntityTouchService());

		addPart(new Target());
		addPart(new Player());
		
		addPart(new BadGuy());
		
		entityTouchService.registerNothingTouched(addPart(new WorldTouchHandler()));
		
	}
	
	public void update(float timeElapsed, float framesPerSecond, Canvas canvas) {
//		Log.d(LOGTAG,"World updating");
		
		UpdateAction update = UpdateAction.getInstance(timeElapsed, framesPerSecond);
		update.apply(this);

		canvas.drawRGB(255, 255, 255);
		
		DrawAction draw = DrawAction.getInstance(canvas);
		draw.apply(drawService);
	}

	public void onKeyDown(int key) {
		if (key == KeyEvent.KEYCODE_D) {
			Log.i(LOGTAG,"This would be a neat place for a breakpoint");
		}
	}

	public void onKeyUp(int key) {
	}
	
	public void onTouch(MotionEvent event) {
		TouchAction touchAction = TouchAction.getInstance(event);
		entityTouchService.handle(touchAction);
	}
	
	public static int SCREEN_WIDTH = 320;
	public static int SCREEN_HEIGHT = 480;

}
